/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 8/30/13
 * Time: 8:35 PM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.state.codeExercise.view.testCase
{
import com.societyGames.codeAlong.state.codeExercise.model.TestCaseStatus;
import com.societyGames.codeAlong.state.codeExercise.model.testCase.TestCase;
import com.societyGames.flashForms.FourStateButton;
import com.societyGames.flashForms.ItemsContainer;
import com.societyGames.flashForms.ZeroOriginContainer;
import com.societyGames.system.EventObject;
import com.societyGames.utility.Bouncer;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;

public class TestCaseButton extends ItemsContainer
{
  private var _testCase:TestCase;
  private var _testCaseIcon:TestCaseIcon;
  private var _fourStateButton:FourStateButton;
  private var _buttonClickedEventObject:EventObject;

  [Embed(source="../../../../../../../../assets/InterfaceAssets.swf", symbol="TestCaseInactive")]
  private static const _inactiveClass:Class;

  [Embed(source="../../../../../../../../assets/InterfaceAssets.swf", symbol="TestCaseOver")]
  private static const _overClass:Class;

  [Embed(source="../../../../../../../../assets/InterfaceAssets.swf", symbol="TestCaseDown")]
  private static const _downClass:Class;

  private var _bouncer:Bouncer;
  private static const BOUNCE_TIME:Number = 1.0;
  private static const BOUNCE_HEIGHT_PERCENT:Number = 1;

  public function TestCaseButton(testCase:TestCase)
  {
    _testCase = testCase;
    _testCase.testCaseStatusChangedEventObject.addEventListener(testCaseStatusChangedHandler);
    _buttonClickedEventObject = new EventObject(this, "BUTTON_CLICKED", Event);

    initializeInterface();
    _testCaseIcon.showNone();

    _bouncer = new Bouncer(this);
  }

  private function initializeInterface():void
  {
    _testCaseIcon = new TestCaseIcon(_testCase.index);
    _testCaseIcon.mouseEnabled = false;
    _testCaseIcon.mouseChildren = false;
    addItem(_testCaseIcon);

    var inactive:Sprite = wrapImageClassInZeroOriginContainer(_downClass); //new BasicAssets.DisabledButtonClass();
    var up:Sprite = wrapImageClassInZeroOriginContainer(_inactiveClass);
    var over:Sprite = wrapImageClassInZeroOriginContainer(_overClass);
    var down:Sprite = wrapImageClassInZeroOriginContainer(_downClass);
    _fourStateButton = new FourStateButton(inactive, up, over, down);
    _fourStateButton.buttonClickEventObject.addEventListener(buttonClickHandler);
    addItem(_fourStateButton);
  }

  private function wrapImageClassInZeroOriginContainer(imageClass:Class):ZeroOriginContainer
  {
    return wrapDisplayObjectInZeroOriginContainer(new imageClass());
  }

  private function wrapDisplayObjectInZeroOriginContainer(displayObject:DisplayObject):ZeroOriginContainer
  {
    var nonZeroOriginContainer:ZeroOriginContainer = new ZeroOriginContainer();
    nonZeroOriginContainer.item = DisplayObject(displayObject);
    return nonZeroOriginContainer;
  }

  private function testCaseStatusChangedHandler(event:Event):void
  {
    switch (_testCase.testCaseStatus)
    {
      case TestCaseStatus.exceptioned:
        startBounce(0);
        _testCaseIcon.showException();
        break;
      case TestCaseStatus.unhandledExceptioned:
        startBounce(0);
        _testCaseIcon.showException();
        break;
      case TestCaseStatus.stopped:
        startBounce(0);
        _testCaseIcon.showFail();
        break;
      case TestCaseStatus.failed:
        startBounce(0);
        _testCaseIcon.showFail();
        break;
      case TestCaseStatus.succeeded:
        _testCaseIcon.showSuccess();
        break;
      case TestCaseStatus.resetting:
        stopBounce();
        //_testCaseIcon.showNone();
        _testCaseIcon.showReady();
        break;
    }
  }

  private function buttonClickHandler(event:Event):void
  {
    this._buttonClickedEventObject.dispatchEvent();
  }

  public function get buttonClickEventObject():EventObject
  {
    return this._buttonClickedEventObject;
  }

  public function get enabled():Boolean
  {
    return _fourStateButton.enabled;
  }

  public function set enabled(value:Boolean):void
  {
    _fourStateButton.enabled = value;
  }

  public function get testCaseIndex():int
  {
    return _testCase.index;
  }

  public function startBounce(delayTimePercent:Number):void
  {
    _bouncer.bounceBottom = this.y;
    _bouncer.bounceTop = -this.height * BOUNCE_HEIGHT_PERCENT;
    _bouncer.bounceTime = BOUNCE_TIME;
    _bouncer.bounce(delayTimePercent * BOUNCE_TIME);
  }

  public function stopBounce():void
  {
    _bouncer.stop();
  }
}
}